﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Threading;
using System.Media;
using System.Text.RegularExpressions;
using System.Reflection;
using DrivingAlarm.Website;
namespace DrivingAlarm
{
    public partial class FrmMain : Form
    {
        private SftMgr sftMgr;
        public FrmMain()
        {
            InitializeComponent();
        }
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case (int)WM_MSG.WM_MSG_UPDATE_INFO:
                    MSG_STRUCT info = new MSG_STRUCT();
                    info = (MSG_STRUCT)m.GetLParam(info.GetType());
                    AddDisplayInfo(info);
                    break;
                default:
                    break;
            }
            base.WndProc(ref m);
        }

        private delegate void dg_AddDisplayInfo(MSG_STRUCT info);
        private void AddDisplayInfo(MSG_STRUCT info)
        {
            if (this.ListInfo.InvokeRequired)
            {
                dg_AddDisplayInfo method = AddDisplayInfo;
                this.Invoke(method,info);
            }
            else
            {
                AddDisplayStr(info.MsgStr);
                if (info.bPlaySound)
                {
                    PlaySound();
                    SetBackColor(Color.Red);
                }
                else
                {
                    SetBackColor(SystemColors.Control);
                }
            }
            this.ListInfo.SelectedIndex = this.ListInfo.Items.Count - 1;
            if (this.ListInfo.Items.Count > 50)
            {
                this.ListInfo.Items.Clear();
            }
        }
        private delegate void dg_SetBackColor(Color color);
        private void SetBackColor(Color color)
        {
            if (this.InvokeRequired)
            {
                dg_SetBackColor method = SetBackColor;
                Invoke(method, color);
            }
            else
            {
                this.BackColor = color;
            }
        }
        private delegate void dg_AddDisplayStr(string str);
        private void AddDisplayStr(string str)
        {
            if (this.ListInfo.InvokeRequired)
            {
                dg_AddDisplayStr method = AddDisplayStr;
                this.Invoke(method, str);
            }
            else
            {
                this.ListInfo.Items.Add(DateTime.Now.ToString("HH:mm:ss  ") + str);
            }
            this.ListInfo.SelectedIndex = this.ListInfo.Items.Count - 1;
            if (this.ListInfo.Items.Count > 50)
            {
                this.ListInfo.Items.Clear();
            }
        }

        private delegate void dl_SetFrmBackColor(Color color);
        private void SetFrmBackColor(Color color)
        {
            if (this.InvokeRequired)
            {
                dl_SetFrmBackColor method = SetFrmBackColor;
                Invoke(method, color);
                return;
            }
            this.grpMain.BackColor = color;
        }
        private delegate void dg_LstClear();
        private void LstClear()
        {
            if (this.ListInfo.InvokeRequired)
            {
                dg_LstClear method = LstClear;
                Invoke(method);
            }
            else
            {
                this.ListInfo.Items.Clear();
            }
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {

            Console.WriteLine(DateTime.Now.ToString("yyyyMMdd"));
#if DEBUG
            this.TopMost = false;
#endif
            this.ListInfo.Items.Clear();
            InitWatchDays();
            this.Icon = DrivingAlarm.Properties.Resources.ico_main;
#if MOTOR_A
            sftMgr = new SftMgr(Website.WEBSITE_TYPE.HANGZHOU_MOTOR);
#endif
#if MOTOR_B
            sftMgr = new SftMgr(Website.WEBSITE_TYPE.HANGZHOU_MOTOR_B);
#endif
            sftMgr.SetParentHwnd(this.Handle);
            this.Text = sftMgr.GetWebsiteName() + " V" + Funcs.GetSftVersion();
            this.cbbInterval.SelectedIndex = 2;
            this.cbbDays.SelectedIndex = 8;
            //sftMgr.StartWatch();
        }

        private void InitWatchDays()
        {
            this.cbbDays.Items.Clear();
            for (int idx = 1; idx < 20;idx++ )
            {
                this.cbbDays.Items.Add(idx.ToString());
            }
        }
        private void cbbInterval_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (sftMgr != null)
            {
                sftMgr.SetWatchInterval(Convert.ToInt32(this.cbbInterval.Text));
            }
        }
        private void PlaySound()
        {
            SoundPlayer sp = new SoundPlayer(DrivingAlarm.Properties.Resources.Alarm);
            sp.Play();
        }
        private void btnOK_Click(object sender, EventArgs e)
        {
            AddDisplayStr(this.btnOK.Text);
            if (this.btnOK.Text == "开始")
            {
                this.btnOK.Text = "停止";
                
                sftMgr.StartWatch();
            }
            else
            {
                this.btnOK.Text = "开始";
                sftMgr.StopWatch();
            }
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            sftMgr.StopWatch();
        }
        private void cbbDays_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (sftMgr != null)
            {
                sftMgr.SetWatchDay(this.cbbDays.SelectedIndex + 1);
            }
        }

        private void tTimeDisplay_Tick(object sender, EventArgs e)
        {
            this.StatusTimeDis.Text = DateTime.Now.ToString("HH:mm:ss");
        }

        private void CMLst_Click(object sender, EventArgs e)
        {
            LstClear();
        }
    }
}
